#Author: 0x9DEFA478


TargetGDBServerPort=""
TargetADBUSBSerialNumber=""
ExecPath=""

options=$(getopt -o "a" --long TargetGDBServerPort:,TargetADBUSBSerialNumber:,ExecPath: -n $0 -- "$@")
if (( $? != 0 )); then
  exit 1
fi
eval set -- "${options}"
while true
do
  case $1 in
    --TargetGDBServerPort)
      TargetGDBServerPort=$2
      shift 2
    ;;
    --TargetADBUSBSerialNumber)
      TargetADBUSBSerialNumber=$2
      IsOutFile="1"
      shift 2
    ;;
    --ExecPath)
      ExecPath=$2
      shift 2
    ;;
    --)
      shift
      break
    ;;
    *)
      echo "$0 param decode error $1"
      exit 1
    ;;
  esac
done


if [[ $TargetGDBServerPort == "" ]]; then
  echo "\e[31mTargetIP no set!\e[0m"
  exit 1
fi
if [[ $ExecPath == "" ]]; then
  echo "\e[31mExecPath no set!\e[0m"
  exit 1
fi

ADBArgs=""

if [[ $TargetADBUSBSerialNumber != "" ]]; then
  ADBArgs="${ADBArgs}-s ${TargetADBUSBSerialNumber} "
fi

ADBArgArr="${ADBArgs}shell" 

ExecCMD="gdbserver :${TargetGDBServerPort} ${ExecPath} $*"

ArgumentList="${ADBArgArr}"
ArgumentList="${ArgumentList} ${ExecCMD}"


adb $ADBArgs forward tcp:${TargetGDBServerPort} tcp:${TargetGDBServerPort}
if (( $? != 0 )); then
  exit 1
fi


./shell/NewTerminal.sh adb $ArgumentList

